package com.my_test.webmagic.webmagic_modules;

import com.my_test.webmagic.module.entity.*;
import com.my_test.webmagic.module.service.*;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhangyalin@act-telecom.com
 * @since 2019-05-27 16:58
 **/
@Component
public class SearchWebMagicMysqlPipeline implements Pipeline {

    @Resource
    private SearchTieziService searchTieziService;
    @Resource
    private SearchTiebaService searchTiebaService;
    @Resource
    private SearchUserService searchUserService;


    @Override
    public void process(ResultItems resultItems, Task task) {
        String type = resultItems.get("type");
        if ("searchTiezi".equals(type)) {
            List<SearchTiezi> entityList = (List<SearchTiezi>) resultItems.get("entityList");
            searchTieziService.saveBatch(entityList);
        } else if ("searchTieba".equals(type)) {
            List<SearchTieba> entityList = (List<SearchTieba>) resultItems.get("entityList");
            searchTiebaService.saveBatch(entityList);
        } else if ("searchUser".equals(type)) {
            SearchUser entity = (SearchUser) resultItems.get("entity");
            searchUserService.save(entity);
        }
    }
}
